var express = require('express');
var router = express.Router();

var userdao = require('../dao/userdao');
var utils = require('../common/utils');

router.get('/', function (req, res, next) {
    req.session.user = undefined;
    res.render('login', {
        title: 'Express'
    });
});

router.post('/dologin', function (req, res, next) {
    var username = req.body.username;
    var password = req.body.password;

    //var data = dologin(username, password);
    userdao.login(username, password).then(function (data) {
        console.log(data);
        var dbpassword = data[0].password;
        var currentpassword = utils.md5(password + data[0].salt);
        if (dbpassword == currentpassword) {
            req.session.user = data[0]; // 登录成功，设置 session
            return res.json({
                code: 1
            });
        } else {
            return res.json({
                code: 0
            });
        }
    });

});
module.exports = router;